<?php
/**
 * $Author: BEESCMS $
 * ============================================================================
 * 网站地址: http://www.beescms.com
 * 您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/

define('IN_CMS','true');
include('init.php');
//权限判断
$get = $_GET['get'];
$type= $_GET['type'];
if(file_exists(DATA_PATH.'sys_info.php')){include(DATA_PATH.'sys_info.php');}//系统设置
$type_pic=empty($_sys['web_upload_image'])?"gif|jpeg|png|jpg|bmp|pjpeg":$_sys['web_upload_image'];//图片类型
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>图片上传</title>
<link rel="stylesheet" type="text/css" href="template/admin.css"/>
<script type="text/javascript" src="template/images/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#add_num').click(function(){
	$thumb_width="<?php echo $_sys['thump_width'];?>";
	$thumb_height = "<?php echo $_sys['thump_height'];?>";
	$str='<div class="pic"><p><input type="file" name="up[]" />&nbsp;图片说明(alt)：<input type="text" name="pic_alt[]" /><input type="checkbox" onclick="check_is_thumb(this);" value="1" name="thumb[]" style="margin:0 5px;" /><input type="hidden" name="is_thumb[]" value="0" id="is_thumb"/>缩略图&nbsp;&nbsp;宽<input name="thumb_width[]" value="'+$thumb_width+'" style="margin:0 5px; width:40px;" />px&nbsp;&nbsp;高<input name="thumb_height[]" style="margin:0 5px; width:40px;" value="'+$thumb_height+'" />px</p></div>';
	$num=$('#num').val();
	if($num>5){$num=5;}
	$str_p="";
	for($i=1;$i<=$num;$i++){
		$str_p=$str_p+$str;
	}
	$('#pic_contain').html($str_p);
});

//单图
$('#sl').click(function(){
$num=$('.pic_list_ct').find('#pic_sl').size();
for(i=0;i<$num;i++){
	$pic_sl=$('.pic_list_ct').find('#pic_sl').eq(i);
	if($pic_sl.attr('checked')){
		$pic_val=$pic_sl.val();
		//缩略图
		$pic_thumb=$pic_sl.parent().find('#pic_thumb').val();
	}
}
$cl_thumb=$('#cl_thumb').attr('checked');//勾选缩略图
if($cl_thumb){
	if($pic_thumb){
		$pic_val=$pic_thumb;
	}else{
		alert('选择图片没有缩略图');
		return;
	}
}
$get='#<?php echo $get;?>';
$show_str='<a target="_blank" href="../upload/'+$pic_val+'"><img src="../upload/'+$pic_val+'"  height="50" border="1" width="50"/></a>';
$(window.opener.document).find($get).val($pic_val);
$(window.opener.document).find('#show_<?php echo $get;?>').html($show_str);
self.close();
});

//多图
$('#sl_ck').click(function(){
	$num=$('.pic_list_ct').find('#pic_sl').size();
	var $pic_rel='';
	var $show_pic='';
for(i=0;i<$num;i++){
	$pic_sl=$('.pic_list_ct').find('#pic_sl').eq(i);
	if($pic_sl.attr('checked')){
		$pic_rel=$pic_sl.attr('rel');
		$pic_val=$pic_sl.val();
		//取得alt
		$pic_alt=$pic_sl.next('#alt').val();
		$show_pic=$show_pic+'<li id="pic_'+$pic_rel+'"><a href="../upload/'+$pic_val+'" target="_blank"><img src="../upload/'+$pic_val+'" border="0" height="120" width="120"/></a><p><input type="text" style="width:100px;" name="alt" id="alt" value="'+$pic_alt+'"/><img src="template/images/c_alt.gif" style="border:0;cursor:point;" onclick="change_alt(this,\''+$pic_rel+'\')" border="0"/></p><input type="hidden" name="fields[<?php echo $get;?>][]" value="'+$pic_rel+'"/><span onclick="del_pic(\''+$pic_rel+'\',this);">删除</span></li>';
	}
}
$get='#<?php echo $get;?>';
$(window.opener.document).find($get).val($show_pic);
$(window.opener.document).find('.form').find('ul#show_pic').append($show_pic);
self.close();
});

});

function check_is_thumb(n){
	$p=$(n).parent('p');
	if($(n).attr('checked')){
		$p.find('#is_thumb').val('1');
	}else{
		$p.find('#is_thumb').val('0');
	}
}

</script>

<style type="text/css">
body{background:#edf2fa; margin:20px;}
.pic{margin-top:3px; border:1px solid #ccc; padding:8px; background:#FFFFFF}
.pic p{line-height:25px; line-height:25px;}
.pic_list_ct{margin:10px 0;}
.pic_list_ct li{width:80px; height:100px; display:block; float:left; margin-bottom:10px; margin-right:10px; display:inline}
.pic_list li_ct label{display:block; height:20px; line-height:20px;}
.sl_pic{margin-top:10px; height:25px; background:#FFFFFF; border:1px solid #ccc; padding:5px;}
.sl_pic span{padding-left:8px; color:#0000FF}
</style>
</head>

<body>
<?php
$submit=$_POST['uppic'];
if($submit){
$up=$_POST['up'];
$pic_alt=$_POST['pic_alt'];
$is_thumb=$_POST['is_thumb'];
$thumb_width=$_POST['thumb_width'];
$thumb_height=$_POST['thumb_height'];
if(is_array($_FILES['up']['tmp_name'])){
foreach($_FILES['up']['tmp_name'] as $k=>$v){
if(empty($v)){continue;}
$value_arr=array('');
$pic_info=array('');
//有图上传图片
if(is_uploaded_file($v)){
		$is_thumb=intval($is_thumb[$k]);
		$thumb_width=empty($thumb_width[$k])?$_sys['thump_width']:intval($thumb_width[$k]);
		$thumb_height=empty($thumb_height[$k])?$_sys['thump_height']:intval($thumb_height[$k]);
		$pic_info['tmp_name']=$v;
		$pic_info['size']=$_FILES['up']['size'][$k];
		$pic_info['type']=$_FILES['up']['type'][$k];
		$pic_info['name']=$_FILES['up']['name'][$k];
		$value_arr=up_img($pic_info,$_sys['upload_size'],array('image/gif','image/jpeg','image/png','image/jpg','image/bmp','image/pjpeg'),$is_thumb,$thumb_width,$thumb_height,$logo);
		//处理上传后的图片信息
		$pic_name=$value_arr['up_pic_name'];//图片名称空
		$pic_ext=$value_arr['up_pic_ext'];//图片扩展名
		$pic_title = $pic_alt[$k];//图片描述
		$pic_size = $value_arr['up_pic_size'];//图片大小
		$pic_path = $value_arr['up_pic_path'];//上传路径
		$pic_time = $value_arr['up_pic_time'];//上传时间
		$pic_thumb = $value_arr['thumb'];//缩略图
		//入库
$sql="insert into ".DB_PRE."uppics (pic_name,pic_ext,pic_alt,pic_size,pic_path,pic_time,pic_thumb) values ('".$pic_name."','".$pic_ext."','".$pic_title."','".$pic_size."','".$pic_path."','".$pic_time."','".$pic_thumb."')";
$mysql->query($sql);
}

}//循环结束
}

}
?>
<form name="up" action="" method="post" enctype="multipart/form-data">
<p style="line-height:25px;"><input name="num" value="3" id="num" style="width:30px;padding:2px 0;" />&nbsp;&nbsp;&nbsp;<input type="button" id="add_num" class="go" value="增加" />(允许上传的图片类型:<?php echo $type_pic;?>)</p>
<div id="pic_contain">
<div class="pic">
	<p><input type="file" name="up[]" />&nbsp;图片说明(alt)：<input type="text" name="pic_alt[]" /><input type="checkbox" onclick="check_is_thumb(this);" value="1" name="thumb[]" style="margin:0 5px;" /><input type="hidden" name="is_thumb[]" value="0" id="is_thumb"/>缩略图&nbsp;&nbsp;宽<input name="thumb_width[]" id="thumb_width" value="<?php echo $_sys['thump_width'];?>" style="margin:0 5px; width:40px;" />px&nbsp;&nbsp;高<input name="thumb_height[]" id="thumb_height" style="margin:0 5px; width:40px;" value="<?php echo $_sys['thump_height'];?>" />px</p>
</div>
</div>
<p style="margin-top:10px"><input type="submit" value="上传" name="uppic" /></p>
</form>

<!--图片列表-->
<div class="sl_pic">
<input type="button" name="sl_pic" <?php if($type=='radio'){?>id="sl"<?php }elseif($type=='checkbox'){?> id="sl_ck"<?php }?> value="确定选择"  /><?php if($type=='radio'){?><label style="padding-left:15px;"><input type="checkbox" id="cl_thumb" value="1" name="cl_thumb" />缩略图</label><?php }?><span style="padding-left:15px;">选择好图片后，确定按钮</span>
</div>
<?php
$maintb=DB_PRE."uppics";
$page=empty($_GET['page'])?1:intval($_GET['page']);
$pagesize=20;
$pagenum=($page-1)*$pagesize;
$query='&type='.$type.'&get='.$get;
$order='order by m.id desc';
$total_num=$GLOBALS['mysql']->fetch_rows("select m.id from {$maintb} as m ");
$total_page=ceil($total_num/$pagesize);
$sql="select m.* from {$maintb} as m {$order} limit {$pagenum},{$pagesize}";
$rel=$GLOBALS['mysql']->fetch_asc($sql);
if(!empty($rel)){
?>
<ul class="pic_list_ct">
<?php
foreach($rel as $k=>$v){
$pic=$v['pic_path'].$v['pic_name'].'.'.$v['pic_ext'];
$img=CMS_SELF.$pic;
?>
<li><a href="<?php echo $img?>" title="【图片说明】：<?php echo $v['pic_alt']?>【上传时间】：<?php echo date('Y-m-d H:m:s',$v['pic_time']);?>【格式】：<?php echo $v['pic_ext'];?>" target="_blank"><img src="<?php echo $img?>" border="0" width="80" height="80" alt="<?php echo $v['pic_alt']?>" /></a><label><?php if($v['pic_thumb']){?><span style="color:red">缩</span><?php }?><input <?php if($type=='radio'){?>type="radio"<?php }elseif($type=='checkbox'){?> type="checkbox"<?php }?> rel="<?php echo $v['id'];?>" id="pic_sl" value="<?php echo str_replace('upload/','',$pic);?>" name="pic_sl"/><input type="hidden" name="alt" id="alt" value="<?php echo $v['pic_alt'];?>" /><input type="hidden" id="pic_val" value="<?php echo str_replace('upload/','',$pic);?>" /><input type="hidden" id="pic_thumb" name="pic_thumb" value="<?php echo str_replace('upload/','',$v['pic_thumb']);?>" /><?php echo cn_substr($v['pic_alt'],8);?></label></li>
<?php
}
?>
</ul>
<?php
}
?>
<div class="page" style="clear:both">
 	<ul>
		<?php echo page('admin_pic_upload.php',$page,$query,$total_num,$total_page);?>
	</ul>
 </div>
</body>
</html>
